﻿//图片上传和显示
function uploadPic(obj) {
    var upStrKey = "#Pic_" + obj + "_File";
    var hiddenStrKey = "#Pic_" + obj;
    var displayStrKey = "#" + obj + "Pic";
    var picFile = $(upStrKey)[0].files[0];
    var fileSize = 1024 * 1024;
    var txtIDNumber = $("#IDNumber").val();
    if (txtIDNumber == null || txtIDNumber == "") {
        bootbox.alert("上传的图片前请先填写身份证号");
        return;
    }
    if (picFile == null || picFile == "") {
        bootbox.alert("请选择要上传的图片");
        return;
    }
    if (picFile.size > fileSize) {
        bootbox.alert("上传的图片大于1M,请重新上传");
        return;
    }
    var index = picFile.name.lastIndexOf(".");
    var suffix = picFile.name.substr(index + 1);
    if ('jpg'.indexOf(suffix.toLowerCase()) == -1) {
        bootbox.alert("图片格式不正确,请选择jpg格式图片");
        return;
    }
    var newFileName = obj + "_" + txtIDNumber + ".jpg";
    if (picFile != null) {
        var formData = new FormData();
        formData.append('MyFile', picFile);
        $.ajax({
            type: "post",
            url: "/Gas/Apply/SavePicture?newFileName=" + newFileName,
            async: true,
            contentType: false,
            processData: false,
            data: formData,
            dataType: 'json',
            success: function () {
                var version = Math.random();
                var hiddenFileName = "/upload/Images/" + newFileName + "?" + version;
                var hiddenVersionFileName = hiddenFileName + "?" + version;
                var str = "<img id='Img" + obj + "' src='" + hiddenVersionFileName + "' style=' width: 200px; height: 100px;cursor:pointer' onclick='largePic(this)'/>";
                $(displayStrKey).empty();
                $(displayStrKey).append(str);
                $(hiddenStrKey).val(hiddenFileName);
                $.currentWindow().$("#gridList").trigger("reloadGrid");
            },
            error: function () {
                bootbox.alert("上传图片失败");
            }
        });
        for (var key of formData.keys()) {
            formData.delete(key);
        };
    }
}

//图片放大功能
function largePic(obj) {
    var src = $("#" + obj.id).attr("src");//获取当前点击的img元素中的src属性  
    $('#outerdiv').attr('display', 'block');
    $('#bigimg').attr("src", src);//设置#bigimg元素的src属性  
    $('#outerdiv').fadeIn("fast");
    $('#outerdiv').click(function () {//再次点击淡出消失弹出层  
        $(this).fadeOut("fast");
    });
}

//住房类申请资料的表单验证
function checkHouseInfo() {
    //户口的验证
    if ("7" != $("#HouseHoldPosition").val()) {
        //家庭情况照片
        if ("" == $("#HouseHolderName").val() || null == $("#HouseHolderName").val()) {
            bootbox.alert("请填写户主姓名");
            return false;
        }
        //户主与学生关系
        if ("" == $("#HouseHolderRelation").val() || null == $("#HouseHolderRelation").val() || "0" == $("#HouseHolderRelation").val()) {
            bootbox.alert("请选择户主与学生关系");
            return false;
        }
    }
    //户主与学生关系
    if ("1" == $("#HouseHolderRelation").val()) {
        //户口本户主页照片
        if ("" == $("#Pic_HouseHold").val() || null == $("#Pic_HouseHold").val()) {
            bootbox.alert("请上传户口本户主页照片");
            return false;
        }
        ////户口本索引页照片
        //if ("" == $("#Pic_HouseHoldIndex").val() || null == $("#Pic_HouseHoldIndex").val()) {
        //    bootbox.alert("请上传户口本索引页照片");
        //    return false;
        //}
        //户口本学生本人页照片
        if ("" == $("#Pic_HouseHoldStudent").val() || null == $("#Pic_HouseHoldStudent").val()) {
            bootbox.alert("请上传户口本学生本人页照片");
            return false;
        }
    }
    else {
        //学生出生证明照片
        if ("" == $("#Pic_BirthCertification").val() || null == $("#Pic_BirthCertification").val()) {
            bootbox.alert("请上传学生出生证明照片");
            return false;
        }
    }

    //住房类申请资料的表单验证
    var HouseFlag = $("#HouseFlag").val();
    if ("" == HouseFlag || "0" == HouseFlag) {
        bootbox.alert("请选择住房类型");
        return false;
    }
    if ("1" == HouseFlag) {
        if ("" == $("#A_CertificateName").val() || null == $("#A_CertificateName").val()) {
            bootbox.alert("请填写房证产权人姓名");
            return false;
        }
        if ("" == $("#A_CertificateIDNumber").val() || null == $("#A_CertificateIDNumber").val()) {
            bootbox.alert("请填写房证产权人身份证号");
            return false;
        }
        if ("" == $("#A_CertificateAddress").val() || null == $("#A_CertificateAddress").val()) {
            bootbox.alert("请填写房证上登记地址");
            return false;
        }
        if ("" == $("#Pic_A_Certificate").val() || null == $("#Pic_A_Certificate").val()) {
            bootbox.alert("请上传房产证照片");
            return false;
        }
        if ("" == $("#A_CertificateRelation").val() || null == $("#A_CertificateRelation").val()) {
            bootbox.alert("请选择房屋所有权人与学生关系");
            return false;
        }
    }
    if ("2" == HouseFlag) {
        if ("" == $("#A_CertificateName2").val() || null == $("#A_CertificateName2").val()) {
            bootbox.alert("请填写购房人姓名");
            return false;
        }
        if ("" == $("#A_CertificateIDNumber2").val() || null == $("#A_CertificateIDNumber2").val()) {
            bootbox.alert("请填写购房人身份证号");
            return false;
        }
        if ("" == $("#A_CertificateAddress2").val() || null == $("#A_CertificateAddress2").val()) {
            bootbox.alert("请填写购房合同登记地址");
            return false;
        }
        if ("" == $("#Pic_A_Contract").val() || null == $("#Pic_A_Contract").val()) {
            bootbox.alert("请上传购房合同照片");
            return false;
        }
        if ("" == $("#Pic_A_Receipt").val() || null == $("#Pic_A_Receipt").val()) {
            bootbox.alert("请上传全款发票照片");
            return false;
        }
    }
    if ("4" == HouseFlag) {
        if ("" == $("#C_RentAddress").val() || null == $("#C_RentAddress").val()) {
            bootbox.alert("请填写租房地址");
            return false;
        }
        if ("" == $("#Pic_C_RentContract").val() || null == $("#Pic_C_RentContract").val()) {
            bootbox.alert("请上传房管局备案的租赁合同照片");
            return false;
        }
    }
    if ("5" == HouseFlag) {
        if ("" == $("#C_RentAddress1").val() || null == $("#C_RentAddress1").val()) {
            bootbox.alert("请填写现居住地址");
            return false;
        }
        if ("" == $("#Pic_C_ResidencePermit").val() || null == $("#Pic_C_ResidencePermit").val()) {
            bootbox.alert("请上传父亲居住证");
            return false;
        }
        if ("" == $("#Pic_C_FemaleResidencePermit").val() || null == $("#Pic_C_FemaleResidencePermit").val()) {
            bootbox.alert("请上传母亲居住证");
            return false;
        }
    }
    return true;
}

//根据住房区分动态取得可见的学校
function getSchool() {
    var houseFlag = $("#HouseFlag").val();
    var classValue = $("#Class").val();

    $("#SchoolId").empty();
    $("#SecondSchoolId").empty();
    //重新绑定下拉框列表
    $("#SchoolId").bindSelect({
        url: "/Gas/Apply/GetEnableSchoolSelectJson",
        param: { houseFlag: houseFlag, classValue: classValue }
    });
    $("#SecondSchoolId").bindSelect({
        url: "/Gas/Apply/GetEnableSchoolSelectJson",
        param: { houseFlag: houseFlag, classValue: classValue }
    });
    $("#SchoolId").next().addClass("myWidth");
    $("#SecondSchoolId").next().addClass("myWidth");

    //动态设定房屋信息部门div的表示
    //sel_div(houseFlag);
    //清空以及隐藏学校信息
    $("#tdContentAmount").text("");
    $("#tdTitleAmount").css('display', 'none');
}

//房屋类型的显示和隐藏
function sel_div(obj) {
    var obj = $("#HouseFlag").val();
    if (obj == "0") {
        $("#div_A_1").css('display', 'block');
        $("#div_A_2").css('display', 'block');
        $("#div_B_1").css('display', 'block');
        $("#div_B_2").css('display', 'block');
        $("#schoolClassInfo").css('display', 'none');
    }
    if (obj == "1") {
        $("#div_A_1").css('display', 'block');
        $("#div_A_2").css('display', 'none');
        $("#div_B_1").css('display', 'none');
        $("#div_B_2").css('display', 'none');
        $("#schoolClassInfo").css('display', 'none');
    }
    if (obj == "2") {
        $("#div_A_1").css('display', 'none');
        $("#div_A_2").css('display', 'block');
        $("#div_B_1").css('display', 'none');
        $("#div_B_2").css('display', 'none');
        $("#schoolClassInfo").css('display', 'none');
    }
    if (obj == "4") {
        $("#div_A_1").css('display', 'none');
        $("#div_A_2").css('display', 'none')
        $("#div_B_1").css('display', 'block');
        $("#div_B_2").css('display', 'none');
        $("#schoolClassInfo").css('display', 'block');
    }
    if (obj == "5") {
        $("#div_A_1").css('display', 'none');
        $("#div_A_2").css('display', 'none');
        $("#div_B_1").css('display', 'none');
        $("#div_B_2").css('display', 'block');
        $("#schoolClassInfo").css('display', 'block');
    }
    //设置房屋大类的表示
    setHouseClass(obj);
}

//设置房屋大类的表示
function setHouseClass(obj) {
    if (obj == "1" || obj == "2") {
        $("#HouseClass").val('有房');
    }
    if (obj == "4" || obj == "5") {
        $("#HouseClass").val('租房');
    }
}

//下拉框初期化设定
function initControl() {
    $("#Class").append("<option value=''>请选择</option>");
    //绑定各种下拉框
    $("#SchoolId").bindSelect({
        url: "/Gas/Apply/GetEnableSchoolSelectJson",
        param: { houseFlag: "" }
    });
    $("#SecondSchoolId").bindSelect({
        url: "/Gas/Apply/GetEnableSchoolSelectJson",
        param: { houseFlag: "" }
    });
    $("#HouseFlag").bindSelect({
        url: "/SystemManage/ItemsData/GetSelectJson",
        param: { enCode: "HouseFlag" }
    });
    $("#Class").bindSelect({
        url: "/SystemManage/ItemsData/GetSelectJson",
        param: { enCode: "Class" }
    });
    $("#FamilySituation").bindSelect({
        url: "/SystemManage/ItemsData/GetSelectJson",
        param: { enCode: "FamilySituation" }
    });
    $("#HouseHoldPosition").bindSelect({
        url: "/SystemManage/ItemsData/GetSelectJson",
        param: { enCode: "HouseHoldPosition" }
    });
    $("#HouseHolderRelation").bindSelect({
        url: "/SystemManage/ItemsData/GetSelectJson",
        param: { enCode: "HouseHolderRelation" }
    });
    $("#OStatus").bindSelect({
        url: "/SystemManage/ItemsData/GetSelectJson",
        param: { enCode: "schoolOStatus" }
    });
    $("#A_CertificateRelation").bindSelect({
        url: "/SystemManage/ItemsData/GetSelectJson",
        param: { enCode: "A_CertificateRelation" }
    });

    //select框加入宽度控制
    $("#SchoolId").next().addClass("myWidth");
    $("#SecondSchoolId").next().addClass("myWidth");
    $("#HouseFlag").next().addClass("myWidth");
    $("#Class").next().addClass("myWidth");
    $("#FamilySituation").next().addClass("myWidth");
    $("#HouseHoldPosition").next().addClass("myWidth");
    $("#HouseHolderRelation").next().addClass("myWidth");
    $("#OStatus").next().addClass("myWidth");
    $("#HouseClass").attr("disabled", true);
    $("#A_CertificateRelation").next().addClass("myWidth");
}

//更新或者详情页的图片初始化
function initPicDiv(data) {
    if ('undefined' != data.Pic_FamilySituation || null != data.Pic_FamilySituation || "" != data.Pic_FamilySituation) {
        updatePicDiv("FamilySituation", data.Pic_FamilySituation);
    }
    if ('undefined' != data.Pic_HouseHold || null != data.Pic_HouseHold || "" != data.Pic_HouseHold) {
        updatePicDiv("HouseHold", data.Pic_HouseHold);
    }
    if ('undefined' != data.Pic_HouseHoldIndex || null != data.Pic_HouseHoldIndex || "" != data.Pic_HouseHoldIndex) {
        updatePicDiv("HouseHoldIndex", data.Pic_HouseHoldIndex);
    }
    if ('undefined' != data.Pic_A_Certificate || null != data.Pic_A_Certificate || "" != data.Pic_A_Certificate) {
        updatePicDiv("A_Certificate", data.Pic_A_Certificate);
    }
    if ('undefined' != data.Pic_A_Contract || null != data.Pic_A_Contract || "" != data.Pic_A_Contract) {
        updatePicDiv("A_Contract", data.Pic_A_Contract);
    }
    if ('undefined' != data.Pic_A_Contract1 || null != data.Pic_A_Contract1 || "" != data.Pic_A_Contract1) {
        updatePicDiv("A_Contract1", data.Pic_A_Contract1);
    }
    if ('undefined' != data.Pic_A_Contract2|| null != data.Pic_A_Contract2 || "" != data.Pic_A_Contract2) {
        updatePicDiv("A_Contract2", data.Pic_A_Contract2);
    }
    if ('undefined' != data.Pic_A_Contract3 || null != data.Pic_A_Contract3 || "" != data.Pic_A_Contract3) {
        updatePicDiv("A_Contract3", data.Pic_A_Contract3);
    }
    if ('undefined' != data.Pic_A_Contract4 || null != data.Pic_A_Contract4 || "" != data.Pic_A_Contract4) {
        updatePicDiv("A_Contract4", data.Pic_A_Contract4);
    }
    if ('undefined' != data.Pic_A_Receipt || null != data.Pic_A_Receipt || "" != data.Pic_A_Receipt) {
        updatePicDiv("A_Receipt", data.Pic_A_Receipt);
    }
    if ('undefined' != data.Pic_C_RentContract || null != data.Pic_C_RentContract || "" != data.Pic_C_RentContract) {
        updatePicDiv("C_RentContract", data.Pic_C_RentContract);
    }
    if ('undefined' != data.Pic_C_ResidencePermit || null != data.Pic_C_ResidencePermit || "" != data.Pic_C_ResidencePermit) {
        updatePicDiv("C_ResidencePermit", data.Pic_C_ResidencePermit);
    }
    if ('undefined' != data.Pic_C_FemaleResidencePermit || null != data.Pic_C_FemaleResidencePermit || "" != data.Pic_C_FemaleResidencePermit) {
        updatePicDiv("C_FemaleResidencePermit", data.Pic_C_FemaleResidencePermit);
    }
    if ('undefined' != data.Pic_MarriageCertificate || null != data.Pic_MarriageCertificate || "" != data.Pic_MarriageCertificate) {
        updatePicDiv("MarriageCertificate", data.Pic_MarriageCertificate);
    }
    if ('undefined' != data.Pic_HouseHoldStudent || null != data.Pic_HouseHoldStudent || "" != data.Pic_HouseHoldStudent) {
        updatePicDiv("HouseHoldStudent", data.Pic_HouseHoldStudent);
    }
    if ('undefined' != data.Pic_BirthCertification || null != data.Pic_BirthCertification || "" != data.Pic_BirthCertification) {
        updatePicDiv("BirthCertification", data.Pic_BirthCertification);
    }
    if ('undefined' != data.Pic_C_MaleLaborContract_IN || null != data.Pic_C_MaleLaborContract_IN || "" != data.Pic_C_MaleLaborContract_IN) {
        updatePicDiv("C_MaleLaborContract_IN", data.Pic_C_MaleLaborContract_IN);
    }
    if ('undefined' != data.Pic_C_FemaleLaborContract_IN || null != data.Pic_C_FemaleLaborContract_IN || "" != data.Pic_C_FemaleLaborContract_IN) {
        updatePicDiv("C_FemaleLaborContract_IN", data.Pic_C_FemaleLaborContract_IN);
    }
    if ('undefined' != data.Pic_C_MaleLaborContract_OUT || null != data.Pic_C_MaleLaborContract_OUT || "" != data.Pic_C_MaleLaborContract_OUT) {
        updatePicDiv("C_MaleLaborContract_OUT", data.Pic_C_MaleLaborContract_OUT);
    }
    if ('undefined' != data.Pic_C_FemaleLaborContract_OUT || null != data.Pic_C_FemaleLaborContract_OUT || "" != data.Pic_C_FemaleLaborContract_OUT) {
        updatePicDiv("C_FemaleLaborContract_OUT", data.Pic_C_FemaleLaborContract_OUT);
    }
}
function updatePicDiv(key, pic_path) {
    var hiddenStrKey = "#Pic_" + key;
    var displayStrKey = "#" + key + "Pic";
    if (null != pic_path && "" != pic_path) {
        var version = Math.random();
        var hiddenVersionFileName = pic_path + "?" + version;
        var str = "<img id='Img" + key + "'src='" + hiddenVersionFileName + "' style=' width: 200px; height: 100px;cursor:pointer' onclick='largePic(this)'/>";
        $(displayStrKey).empty();
        $(displayStrKey).append(str);
        $(hiddenStrKey).val(pic_path);
    }
}

//下拉框联动的情况下需要重新填充
function fillInSelect(data) {
    //重新填充志愿学校
    $("#SchoolId option[value='" + data.SchoolId + "']").attr('selected', 'selected');
    $("#SecondSchoolId option[value='" + data.SecondSchoolId + "']").attr('selected', 'selected');
    $("#select2-SchoolId-container").text($("#SchoolId option[value='" + data.SchoolId + "']").text());
    $("#select2-SecondSchoolId-container").text($("#SecondSchoolId option[value='" + data.SecondSchoolId + "']").text());
}

// 根据第三方接口验证住房信息
function btn_CheckHouseInfo() {
    var houseFlag = $("#HouseFlag").val();
    var pwdKey = $("#pwdKey").val();
    if (null == pwdKey || "" == pwdKey) {
        if ("1" == houseFlag) {
            var CertificateName = $("#A_CertificateName").val();
            var CertificateIDNumber = $("#A_CertificateIDNumber").val();
            var CertificateAddress = $("#A_CertificateAddress").val();
            if ("" == CertificateName || null == CertificateName) {
                bootbox.alert("请填写房证产权人姓名");
                return false;
            }
            if ("" == CertificateIDNumber || null == CertificateIDNumber) {
                bootbox.alert("请填写房证产权人身份证号");
                return false;
            }
            if ("" == CertificateAddress || null == CertificateAddress) {
                bootbox.alert("请填写房证上登记地址");
                return false;
            }
            $.submitForm({
                url: "/Gas/Apply/GetAddressExisInfor?username=" + CertificateName + "&idnumber=" + CertificateIDNumber + "&address=" + CertificateAddress,
                success: function (data) {
                    if ("1" == data.data) {
                        $("#A1_Result").text("验证通过");
                        $("#A1_Result").css("color", "#1ABC9C!important");
                        $("#schoolClassInfo").css('display', 'block');
                    } else {
                        $("#A1_Result").text("验证未通过");
                        $("#A1_Result").css("color", "#FF0000");
                        $("#schoolClassInfo").css('display', 'none');
                    }
                }
            });
        }
        if ("2" == houseFlag) {
            var CertificateIDNumber2 = $("#A_CertificateIDNumber2").val();
            if ("" == CertificateIDNumber2 || null == CertificateIDNumber2) {
                bootbox.alert("请填写购房人身份证号");
                return false;
            }
            $.submitForm({
                url: "/Gas/Apply/GetContractInfor?idnumber=" + CertificateIDNumber2,
                success: function (data) {
                    if (null == data.data || "" == data.data || 0 == data.data.length) {
                        $("#A2_Result").text("验证未通过");
                        $("#A2_Result").css("color", "#FF0000");
                        $("#schoolClassInfo").css('display', 'none');
                    } else {
                        $("#A2_Result").text("验证通过");
                        $("#A1_Result").css("color", "#1ABC9C!important");
                        $("#schoolClassInfo").css('display', 'block');
                    }
                }
            });
        }
    } else {
        $("#schoolClassInfo").css('display', 'block');
    }
}

function checkHouseInfoSpan() {
    // 验证房产信息
    var houseFlag = $("#HouseFlag").val();
    var pwdKey = $("#pwdKey").val();
    if (null == pwdKey || "" == pwdKey) {
        if ("1" == houseFlag) {
            if ("验证通过" != $("#A1_Result").text()) {
                bootbox.alert("房产信息验证未通过，请确认");
                return false;
            }
        }
        if ("2" == houseFlag) {
            if ("验证通过" != $("#A2_Result").text()) {
                bootbox.alert("房产信息验证未通过，请确认");
                return false;
            }
        }
    }
    return true;
}

function getAmount(obj) {
    var schoolId = obj.value;
    var classValue = $("#Class").val();
    if ("" == schoolId || null == schoolId) {
        $("#tdContentAmount").text("");
        $("#tdTitleAmount").css('display', 'none');
        return false;
    }

    if ("" == classValue || null == classValue) {
        $("#tdContentAmount").text("");
        $("#tdTitleAmount").css('display', 'none');
        return false;
    }

    $.ajax({
        url: "/Gas/Apply/GetReportDataJson",
        data: { schoolId: schoolId, classValue: classValue, ajaxFlag:"1"},
        dataType: "json",
        async: false,
        success: function (data) {
            if (null != data || 'undefined' != data) {
                if (data.rows.length > 0) {
                    $("#tdContentAmount").text("当前学校的该年级学位总数：" + data.rows[0].ClassFreeTotalAmount + " 已申报人数：" + data.rows[0].ApplyAmount );
                    $("#tdTitleAmount").css('display', 'block');
                }
            }
        }
    });
}